<?php

/**
 * @name Log
 * @author Roman Metlinskyi <r.metlinskyi@gmail.com>
 */
class Log
{

  private static $_log;
  private $logs;
  public $ToWeb;
  public $ToFile;

  private function __construct()
  {
    $this->ToWeb = true;
    $this->logs = array();
  }

  private function __clone()
  {

  }

  public static function Log()
  {
    return (!isset(self::$_log)) ? (self::$_log = new self()) : self::$_log;
  }

  public function dump($value)
  {
    $this->add(__METHOD__, $value);
  }

  public function warning($message)
  {
    $this->add(__METHOD__, $message);
  }

  public function exception(Exception $e)
  {
    $this->add(__METHOD__, $e);
  }

  private function add($key, $value)
  {
    $this->logs[$key][] = $value;
  }

  public function __destruct()
  {
    if (!$this->ToWeb && !$this->ToFile)
      return;

    foreach ($this->logs as $type => $value) {
      echo $this->ToWeb($type, $value[0]);
    }
  }

  private function ToWeb($type, $value)
  {
    if ($this->ToWeb)
      echo $this->fieldset($type, $this->pre(print_r($value, true)));
  }

  private function fieldset($name, $value)
  {
    if (!empty($name))
      $name = "<legend>{$name}</legend>";

    return "<fieldset style='font-size:8pt;'>{$name}{$value}</fieldset>";
  }

  private function pre($value)
  {
    return "<pre style='font-family:monospace'>{$value}</pre>";
  }

}

